<?php
	require('adaa.doc.php');
	$app->post(
		'/adaa/transfer',
		function() use($app, $db2){
			$data = $app->request()->post();
			try{
				$db2->beginTransaction();
				$echo['error'] = "";
				if($data && isset($data['userid']) && isset($data['usertype']) && isset($data['transfers_userid']) && isset($data['transfers_usertype']) && isset($data['tradeadaa']) && isset($data['pwd'])){
					if($data['userid']==$data['transfers_userid'] && $data['usertype']==$data['transfers_usertype']){
						$echo['error'] = "不允许转账给自己";
						echo json_encode($echo);exit;
					}
					$tb = $data['usertype'] == 1 ? 'tb_member' : 'tb_coach';
					$user=$db2->select(1,$tb,"*",array('paypassword'=>$data['pwd'],'tbid'=>$data['userid']));
					if(empty($user['tbid'])){
						$echo['error'] = "支付密码错误";
						echo json_encode($echo);exit;
					}else{
						if($user['adaa']<$data['tradeadaa']){
							$echo['error'] = "用户adaa币不足";
							echo json_encode($echo);exit;
						}
					}
					$tb = $data['transfers_usertype'] == 1 ? 'tb_member' : 'tb_coach';
					$user=$db2->select(2,$tb,"*",array('tbid'=>$data['transfers_userid']));
					if($user==0){
						$echo['error'] = "转账用户不存在";
						echo json_encode($echo);exit;
					}
					$adaaid=getordernum("AD");
					$pay=array('type'=>1,'userid'=>$data['userid'],'usertype'=>$data['usertype'],'tradeadaa'=>$data['tradeadaa'],'adaaid'=>$adaaid,'transfers_userid'=>$data['transfers_userid'],'transfers_usertype'=>$data['transfers_usertype']);
					if(!empty($data['remark'])){
						$pay['remark']=$remark;
					}
					creatadaa($pay);
					updateadaa($adaaid,1);
				}else{
					$echo['error'] = "数据缺失，请检查";
				}
				$db2->commit();
			}catch(Exception $e){
				$db2->catche($e);
				exit;
			}
			echo json_encode($echo);
		}
	);
	$app->get(
		'/adaa/get',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['userid']) && isset($data['usertype'])  && isset($data['from']) && isset($data['to'])){
				$tb = $data['usertype'] == 1 ? 'tb_member' : 'tb_coach';
				$member=$db2->select(1, $tb, '*', array('tbid' => $data['userid']));
				if(!empty($member['tbid'])){
					$sqlwhere=array('userid' => $data['userid'],'order'=>'tbid desc');
					$echo['count'] = $db2->select(2, 'tb_adaadetail', '*', $sqlwhere);
					$sqlwhere['limit'] = array($data['from'], $data['to']);
					$trade=$db2->select(0, 'tb_adaadetail', '*', $sqlwhere);
					foreach($trade as $value){
						if($value['type']==0){
							$tmp['content']='签到 成功';
						}else if($value['type']==1){
							$adaa=$db2->select(1,'tb_adaa','*',array('adaaid'=>$value['tradeid']));
							if($value['trademoney']<0){
								$tb = $adaa['transfers_usertype'] == 1 ? 'tb_member' : 'tb_coach';
								$nick=$db2->select(3,$tb,'nickname',array('tbid'=>$adaa['transfers_userid']));
								$tmp['content']='转账给'.$nick.' 成功';
							}else{
								$tb = $adaa['usertype'] == 1 ? 'tb_member' : 'tb_coach';
								$nick=$db2->select(3,$tb,'nickname',array('tbid'=>$adaa['userid']));
								$tmp['content']=$nick.'转账给你 成功';
							}
						}else if($value['type']==2){
							$tmp['content']='支付 成功';
						}else if($value['type']==3){
							$tmp['content']='充值余额 成功';
						}
						$tmp['dt']=date('Y-m-d',strtotime($value['creatdate']));
						$tmp['time']=date('H:i',strtotime($value['creatdate']));
						$tmp['addd']=$value['trademoney'];
						$week=date('w',strtotime($value['creatdate']));
						switch($week){
							case 0:$week='日';break;
							case 1:$week='一';break;
							case 2:$week='二';break;
							case 3:$week='三';break;
							case 4:$week='四';break;
							case 5:$week='五';break;
							case 6:$week='六';break;
						}
						$tmp['week']='星期'.$week;
						$echo['data'][]=$tmp;
					}
				}else{
					$echo['error'] = "用户不存在";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);	
?>